package com.sgb.member.view_model;

import com.sgb.member.MemberConfig;
import com.sgb.member.di.repository.MemberRepertory;
import com.sgb.member.entity.ClientDynamicStateBean;
import com.sgb.member.entity.ListMResponse;
import com.sgb.member.ui.activity.ClientDynamicStateActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

/**
 * Created by：冯齐 on 2021/1/26 16:57
 * Description：
 */
public class ClientDynamicStateModel extends BaseViewModel {
    ClientDynamicStateActivity activity;
    MemberRepertory repertory;
    private int current;
    public ClientDynamicStateModel(ClientDynamicStateActivity activity, MemberRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;

    }

    // 会员经营企业动态
    public void getMemberCompanyNews(boolean isLoadMore, String searchTxt, String no1, String no2, String no4, String no5) {
        int currentPage = isLoadMore ? current + 1 : 1;
        Map<String, Object> map = new HashMap<>();
        map.put("search", searchTxt); //经营类型
        map.put("level", "");  //会员等级
        map.put("limit", 20);
        map.put("page", currentPage);
        map.put("provinceNo", no4);
        map.put("cityNo", no5);
        map.put("industryNameNo", no1); // 	行业分类no
        map.put("dynamicTypeNo", no2); // 动态类别no
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getMemberCompanyNews(map).enqueue(new ICallBack<BaseEntity<ListMResponse<List<ClientDynamicStateBean>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListMResponse<List<ClientDynamicStateBean>>> data) {
                current = data.getData().getCurrent();
                int total = data.getData().getPages();
                activity.refreshRecycler(data.getData().getList(), isLoadMore, current >= total);
            }

            @Override
            public void onFailure(Call<BaseEntity<ListMResponse<List<ClientDynamicStateBean>>>> call, Throwable t) {
                super.onFailure(call, t);
                activity.showEmpty();
            }
        });
    }

    // 企业动态-行业分类 no
    public void getDynamicCategoryList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getDynamicCategoryList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setDynamicCategoryList(isShowPopup, data.getData());
            }
        });
    }

    // 企业动态-动态类型
    public void getDynamicList(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getDynamicList().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                activity.setDynamicList(isShowPopup, data.getData());
            }
        });
    }

    /**
     * 获取省市区地址
     */
    public void getAddressModel(boolean isShowPopup) {
        DialogHelper.showProgressDialog(activity, "数据获取中...").setCanceledOnTouchOutside(false);
        repertory.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    List<CommonPopEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> datum : data.getData()) {
                        entities.add(new CommonPopEntity(datum.getNo(), datum.getSimpleName()));
                    }
                    activity.setAddressData(isShowPopup, data.getData(), entities);
                }
            }
        });
    }

    public void getUrlSetting(String param, String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getApi().getUrlSetting(param).enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + id;
                MemberConfig.getInstance().startWebViewX5(activity, url, 101);
            }
        });
    }
}
